<template>
  <div class="equipment-auth-container">
    <div class="page-header">
      <h2>羽毛球装备鉴定</h2>
      <p>上传装备图片并填写相关信息，获取专业鉴定结果</p>
    </div>

    <div class="main-content">
      <!-- 鉴定表单区域 -->
      <div class="auth-form-card">
        <h3>提交鉴定申请</h3>
        <div class="form-group">
          <label for="equipmentType">装备类型</label>
          <select id="equipmentType" v-model="formData.equipmentType" required>
            <option value="">请选择装备类型</option>
            <option value="racket">羽毛球拍</option>
            <option value="shoes">羽毛球鞋</option>
            <option value="clothes">运动服装</option>
            <option value="accessories">配件</option>
          </select>
        </div>

        <div class="form-group">
          <label for="brand">品牌</label>
          <input type="text" id="brand" v-model="formData.brand" placeholder="请输入品牌名称" required>
        </div>

        <div class="form-group">
          <label for="model">型号</label>
          <input type="text" id="model" v-model="formData.model" placeholder="请输入型号" required>
        </div>

        <div class="form-group">
          <label for="purchaseChannel">购买渠道</label>
          <select id="purchaseChannel" v-model="formData.purchaseChannel" required>
            <option value="">请选择购买渠道</option>
            <option value="official">官方旗舰店</option>
            <option value="authorized">授权经销商</option>
            <option value="online">电商平台</option>
            <option value="physical">实体店</option>
            <option value="secondhand">二手平台</option>
            <option value="other">其他渠道</option>
          </select>
        </div>

        <div class="form-group">
          <label>上传图片（至少1张，最多3张）</label>
          <div class="image-upload-area">
            <div class="upload-box" v-if="uploadedImages.length < 3" @click="triggerFileInput">
              <i class="upload-icon">+</i>
              <p>点击上传图片</p>
              <input type="file" ref="fileInput" accept="image/*" multiple @change="handleImageUpload" hidden>
            </div>
            <div class="preview-container" v-for="(image, index) in uploadedImages" :key="index">
              <img :src="image" alt="预览图片">
              <button class="remove-img" @click="removeImage(index)">×</button>
            </div>
          </div>
        </div>

        <div class="form-group">
          <label for="description">其他说明</label>
          <textarea id="description" v-model="formData.description" placeholder="请描述您对装备的疑问或需要鉴定的重点"></textarea>
        </div>

        <div class="submit-section">
          <button class="submit-btn" @click="submitAuthentication" :disabled="!isFormValid || uploadedImages.length === 0">
            提交鉴定申请
          </button>
          <p class="form-tip" v-if="!isLoggedIn">* 登录后即可提交鉴定申请</p>
        </div>
      </div>

      <!-- 鉴定案例展示 -->
      <div class="auth-cases">
        <h3>鉴定案例参考</h3>
        <div class="cases-grid">
          <div class="case-card" v-for="caseItem in authCases" :key="caseItem.id">
            <div class="case-image">
              <img :src="caseItem.imageUrl" :alt="caseItem.title">
            </div>
            <div class="case-content">
              <h4 class="case-title">{{ caseItem.title }}</h4>
              <div class="case-meta">
                <span class="case-type">{{ caseItem.type }}</span>
                <span class="case-date">{{ caseItem.date }}</span>
              </div>
              <div class="case-result">
                <span class="result-label">鉴定结果：</span>
                <span class="result-value" :class="caseItem.result === 'authentic' ? 'authentic' : 'fake'">{{ caseItem.resultText }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';

const store = useStore();
const router = useRouter();
const fileInput = ref(null);

// 表单数据
const formData = ref({
  equipmentType: '',
  brand: '',
  model: '',
  purchaseChannel: '',
  description: ''
});

// 上传图片
const uploadedImages = ref([]);

// 模拟鉴定案例数据
const authCases = ref([
  {
    id: 1,
    title: '尤尼克斯 YONEX ASTROX 99 PRO 鉴定',
    type: '羽毛球拍',
    date: '2025-09-15',
    imageUrl: 'https://img10.360buyimg.com/n5/s720x720_jfs/t1/274189/3/24503/137604/6805eeafF6bcdcb34/5b0e2a995f870a6d.jpg.avif',
    result: 'authentic',
    resultText: '正品'
  },
  {
    id: 2,
    title: '李宁 突袭7C 真伪鉴定',
    type: '羽毛球拍',
    date: '2025-09-10',
    imageUrl: 'https://img11.360buyimg.com/n7/jfs/t1/309852/6/11988/86478/685b5972Fbf793d2d/64ba6fac7df41457.jpg',
    result: 'authentic',
    resultText: '正品'
  },
  {
    id: 3,
    title: '胜利 VICTOR A970ACE 鉴定',
    type: '羽毛球鞋',
    date: '2025-09-05',
    imageUrl: 'https://img14.360buyimg.com/n7/jfs/t1/183930/29/36975/41826/64f7e39fF261efabe/442e722b19f8912b.jpg',
    result: 'fake',
    resultText: '仿品'
  }
]);

// 检查用户登录状态
const isLoggedIn = computed(() => {
  return store.state.isAuthenticated;
});

// 表单验证
const isFormValid = computed(() => {
  return formData.value.equipmentType &&
         formData.value.brand &&
         formData.value.model &&
         formData.value.purchaseChannel;
});

// 触发文件选择
const triggerFileInput = () => {
  if (!isLoggedIn.value) {
    alert('请先登录后再上传图片');
    router.push('/login');
    return;
  }
  fileInput.value.click();
};

// 处理图片上传
const handleImageUpload = (e) => {
  const files = e.target.files;
  if (files && files.length) {
    const maxImages = 3;
    const remainingSlots = maxImages - uploadedImages.value.length;
    const filesToProcess = Math.min(files.length, remainingSlots);

    for (let i = 0; i < filesToProcess; i++) {
      const reader = new FileReader();
      reader.onload = (event) => {
        uploadedImages.value.push(event.target.result);
      };
      reader.readAsDataURL(files[i]);
    }
  }
};

// 移除图片
const removeImage = (index) => {
  uploadedImages.value.splice(index, 1);
};

// 提交鉴定申请
const submitAuthentication = () => {
  if (!isLoggedIn.value) {
    alert('请先登录');
    router.push('/login');
    return;
  }

  if (!isFormValid.value) {
    alert('请填写完整的装备信息');
    return;
  }

  if (uploadedImages.value.length === 0) {
    alert('请至少上传一张图片');
    return;
  }

  // 创建鉴定记录
  const authRecord = {
    id: Date.now(),
    ...formData.value,
    images: [...uploadedImages.value],
    submitDate: new Date().toISOString().split('T')[0],
    status: 'pending',
    userId: store.state.user.id
  };

  // 保存到localStorage
  const existingRecords = JSON.parse(localStorage.getItem('authRecords') || '[]');
  existingRecords.push(authRecord);
  localStorage.setItem('authRecords', JSON.stringify(existingRecords));

  // 重置表单
  formData.value = {
    equipmentType: '',
    brand: '',
    model: '',
    purchaseChannel: '',
    description: ''
  };
  uploadedImages.value = [];

  alert('鉴定申请已提交，我们将尽快为您处理');
};
</script>

<style scoped>
.equipment-auth-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
}

.page-header {
  text-align: center;
  margin-bottom: 2.5rem;
}

.page-header h2 {
  color: #2196F3;
  margin-bottom: 0.5rem;
}

.page-header p {
  color: #666;
  margin: 0;
}

.main-content {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2rem;
}

/* 鉴定表单样式 */
.auth-form-card {
  background-color: white;
  border-radius: 12px;
  padding: 2rem;
  box-shadow: 0 2px 10px rgba(0,0,0,0.05);
}

.auth-form-card h3 {
  color: #333;
  margin-top: 0;
  margin-bottom: 1.5rem;
  padding-bottom: 0.8rem;
  border-bottom: 1px solid #f0f0f0;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  color: #555;
  font-weight: 500;
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 0.8rem;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 1rem;
}

.form-group textarea {
  min-height: 100px;
  resize: vertical;
}

.image-upload-area {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.upload-box {
  width: 120px;
  height: 120px;
  border: 2px dashed #2196F3;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  color: #2196F3;
  transition: all 0.3s;
}

.upload-box:hover {
  background-color: #e3f2fd;
}

.upload-icon {
  font-size: 2rem;
  margin-bottom: 0.5rem;
}

.preview-container {
  width: 120px;
  height: 120px;
  border-radius: 8px;
  overflow: hidden;
  position: relative;
}

.preview-container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.remove-img {
  position: absolute;
  top: 5px;
  right: 5px;
  background-color: rgba(255,255,255,0.8);
  border: none;
  border-radius: 50%;
  width: 24px;
  height: 24px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  font-size: 1rem;
  color: #f44336;
}

.submit-section {
  margin-top: 2rem;
  text-align: center;
}

.submit-btn {
  background-color: #2196F3;
  color: white;
  border: none;
  padding: 0.8rem 2rem;
  border-radius: 6px;
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.3s;
}

.submit-btn:hover {
  background-color: #1976D2;
}

.submit-btn:disabled {
  background-color: #b3e5fc;
  cursor: not-allowed;
}

.form-tip {
  color: #777;
  margin-top: 0.8rem;
  font-size: 0.9rem;
}

/* 鉴定案例样式 */
.auth-cases {
  background-color: white;
  border-radius: 12px;
  padding: 2rem;
  box-shadow: 0 2px 10px rgba(0,0,0,0.05);
}

.auth-cases h3 {
  color: #333;
  margin-top: 0;
  margin-bottom: 1.5rem;
  padding-bottom: 0.8rem;
  border-bottom: 1px solid #f0f0f0;
}

.cases-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1.5rem;
}

.case-card {
  display: flex;
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  overflow: hidden;
  transition: transform 0.3s;
}

.case-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 3px 10px rgba(0,0,0,0.08);
}

.case-image {
  width: 120px;
  height: 120px;
  flex-shrink: 0;
}

.case-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.case-content {
  padding: 1rem;
  flex: 1;
}

.case-title {
  margin: 0 0 0.5rem 0;
  font-size: 1.1rem;
  color: #333;
}

.case-meta {
  display: flex;
  justify-content: space-between;
  color: #777;
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
}

.case-result {
  display: flex;
  align-items: center;
}

.result-label {
  color: #555;
  margin-right: 0.5rem;
}

.result-value {
  font-weight: 500;
}

.authentic {
  color: #4CAF50;
}

.fake {
  color: #f44336;
}

@media (max-width: 768px) {
  .main-content {
    grid-template-columns: 1fr;
  }
}
</style>